var _ROOTURI_ = "/scheduler/test";

function app_start(rootnode)
{
  if(!rootnode)
  {
    rootnode = $("#body");
  }

}

function display_error(error_type, div, where)
{
  setInnerHTML(error_type, div, where);
}

function setInnerHTML(content, dom_node, where)
{
  if(where == "begin")
  {
    $(dom_node).prepend(content);
  }
  else if(where == "end")
  {
    $(dom_node).append(content);
  }
  else
  {
    $(dom_node).empty().append(content);
  }
}

function doLoading(type)
{
  if(type == "topbar") // For the top, fixes loading image being out of bounds
  {
    setInnerHTML("<img src=\""+_ROOTURI_+"/images/loading2.gif\" width=\"16\" height=\"16\" alt=\"\" />", "#topbar_load"); // Uses loading2.gif (vs. loading.gif for other DIVs)
  }
  else
  {
    setInnerHTML("<div class=\"load\"><img src=\""+_ROOTURI_+"/images/loading.gif\" width=\"16\" height=\"16\" alt=\"\" /></div>", "#"+type+"_div");
  }
}

function doLoading2(type)
{
  (type) ? $("#boxload").show() : $("#boxload").hide();
}

function link_to(url, text)
{
  return "<a href=\"#\" onclick=\"goto_page("+url+")\">"+text+"</a>";
}

function button_link_to(url, text)
{
  return $.new("input", {'type': "button", 'value': text}).click(function(){goto_page(url);});
}

function menu_link_to(url, text)
{
  return $.new("li", {}, text).click(function(){goto_page(url);});
}

function reload_topbar()
{
  doLoading("topbar");
  $.getJSON(_ROOTURI_+"/userstatus.html", {}, function(response, textStatus){
      //should return {loggedIn: boolean, username: string, hasSchedule: boolean, hasAdmin: boolean, urls{login: string, logout: string, lostpass: string, changepass: string, admin: string, schedule: string}}
      var tb = $("#topbar_div");
      if(response.loggedIn)
      {
        setInnerHTML("<strong>"+response.username+"</strong> ", "#topbar_div");

        tb.append(button_link_to(response.urls.logout, "logout"));
        tb.append(button_link_to(response.urls.changepass, "change password"));
        if(response.hasSchedule)
        {
          tb.append(button_link_to(response.urls.schedule, "schedule"));
        }
        if(response.hasAdmin)
        {
          tb.append(button_link_to(response.urls.admin, "admin"));
        }
      }
      else
      {
        var umm = $.new("ul");
        var um = $.new("li",{},"Guest");
        um.append(umm);
        var menu = $.new("ul",{},[]);
        menu.append(um);
        umm.append(menu_link_to(response.urls.login, "login"));
        tb.append(menu);
        menu.clickMenu();
        tb.append($.new("input",{'type': "button", 'value': "return home"}).click(MainCallbacks.show_app_main));
      }
      setInnerHTML("", "#topbar_load");
  });//.ajaxError(function(event, XMLHttpRequest, ajaxOptions, thrownError){
     // display_error("-- Loading Error --", "#topbar_div");
  //});
}

function goto_page(the_url, the_target)
{
  if(!the_target)
  {
    the_target = "#main_div";
  }
  doLoading("main");
  $.getJSON(the_url,{},function(response, textStatus){
      //should return {callback: string, data: object}
      if(response.callback)
      {
        window[response.callback](response.data);
      }
      else
      {
        display_error("-- Loading Error --", the_target);
      }
      return response;
  });//.ajaxError(function(event, XMLHttpRequest, ajaxOptions, thrownError){
     // display_error("-- Loading Error --", the_target);
     // return response;
  //});
}
